from _md5 import md5
from dataclasses import dataclass
from datetime import datetime
from hashlib import sha256
from typing import List

from sqlalchemy import Boolean

import models
from exts import db
@dataclass
class Role(db.Model):
    __tablename__ = "role"
    id:int = db.Column(db.Integer, primary_key=True, comment='角色ID')
    name:str = db.Column(db.String(255), comment='角色名称')
    code:str = db.Column(db.String(255), comment='角色标识')
    enable:str = db.Column(Boolean, comment='是否启用',default=True)
    remark:str = db.Column(db.String(255), comment='备注')
    details:str = db.Column(db.String(255), comment='详情')
    sort:str = db.Column(db.Integer, comment='排序')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    #power = db.relationship('Power', secondary="role_power", backref=db.backref('role'))
    def to_dict(self):
        role = {
            "id":self.id,
            "name":self.name,
            "code":self.code,
            "enable":True if self.enable> 0 else False,
            "remark":self.remark,
            "details":self.details,
            "sort":self.sort,
            "create_time":self.create_time,
            "update_time":self.update_time,
        }
        return role
    @classmethod
    def to_model(cls, **kwargs):
        model = Role()  # 实例化一个device对象
        columns = [c.name for c in cls.__table__.columns]  # 获取Devices模型定义的所有列属性的名字
        for k, v in kwargs.items():  # 遍历传入kwargs的键值
            if k in columns:  # 如果键包含在列名中，则为该device对象赋加对应的属性值
                setattr(model, k, v)
        return model